package com.zjgeport.zjgdanger.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.zjgeport.zjgdanger.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IllegalFormatCodePointException;

import q.rorbin.badgeview.QBadgeView;

/**
 * Created by minlong on 2017/9/18.
 */

public class AppGridAdapter extends BaseAdapter {

    private Context main;

    private ArrayList<HashMap<String, Object>> list;

    private int width;

    private Integer illegalCount;

    private Integer acciCount;

    public AppGridAdapter(Context main, ArrayList<HashMap<String, Object>> list,int wid, Integer count1,Integer count2)
    {
        this.main = main;
        this.list = list;
        this.width = wid;
        this.illegalCount = count1;
        this.acciCount = count2;
    }

    public int getCount()
    {
        return list.size();
    }

    public Object getItem(int position)
    {
        return list.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        VHolder vHolder = null;
        // 如果未选择或list列表为空时直接跳出，避免NullPointException
        if (position < 0 || list.size() <= 0)
        {
            return null;
        }
        // 如果为空重新导入ListView的布局文件，布局文件自定义
        if (convertView == null)
        {
            vHolder = new VHolder();
            convertView = LayoutInflater.from(main).inflate(R.layout.app_griditem, null);
            //LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.ll_Name3);
            //convertView.setLayoutParams(new LinearLayout.LayoutParams(Math.abs(width/2),Math.abs(width/2)));
            convertView.setLayoutParams(new AbsListView.LayoutParams(Math.abs(width/2),Math.abs(width/3)));
            vHolder.textView  = (TextView) convertView.findViewById(R.id.app_title);

            vHolder.img  = (ImageView) convertView.findViewById(R.id.app_icon);
            convertView.setTag(vHolder);
        }else
        {
            vHolder = (VHolder) convertView.getTag();
        }

        //
        vHolder.img.setBackgroundResource((Integer) list.get(position).get("ItemImage"));
        vHolder.textView.setText(list.get(position).get("ItemText").toString());
        if ( position == 0)
        {
            new QBadgeView(main).bindTarget(vHolder.img).setBadgeNumber(illegalCount);
        }
        else  if (position == 1)
        {
            new QBadgeView(main).bindTarget(vHolder.img).setBadgeNumber(acciCount);
        }


        return convertView;
    }
    static class VHolder{
        public TextView textView;
        public ImageView img;
    }
}
